package test.mapreduce;

import java.io.IOException;
import java.lang.InterruptedException;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class LetterCount {
	static class LetterCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
		public void map(LongWritable clef, Text valeur, Context contexte) throws IOException, InterruptedException {
			String ligne = valeur.toString().toLowerCase();
			StringTokenizer tokenizer = new StringTokenizer(ligne, " \t\n\r\f:;«».,-?'!|0123456789<=>()&[]$@");
			while (tokenizer.hasMoreTokens()){
				String word = tokenizer.nextToken();
				for (int i = 0; i < word.length(); ++i) {
					char c = word.charAt(i);
					contexte.write(new Text(String.valueOf(c)), new IntWritable(1));
				}
			}
		}
	}
	
	public static class LetterCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		public void reduce(Text clef, Iterable<IntWritable> valeurs, Context contexte) throws IOException, InterruptedException {
			int somme = 0;
			for (IntWritable val : valeurs) {
				somme += val.get();
			}
			contexte.write(clef, new IntWritable(somme));
		}
	}
	
	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			System.err.println(args.length + "(" + args[0] + "," + args[1] + ")");
			System.err.println("Usage : LetterCount <source> <destination>");
			System.exit(-1);
		}
		
		Job job = new Job();
		job.setJarByClass(LetterCount.class);
		
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		// job.setInputFormat(MonInputFormat.class);
		
		job.setMapperClass(LetterCountMapper.class);
		//job.setCombinerClass (ComptageMotCombiner.class) ;
		job.setReducerClass(LetterCountReducer.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		//job.setNumReduceTasks (2) ;
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}
